Only force update git if -u is passed
authorYehuda Katz <wycats@gmail.com>
Tue, 24 Jun 2014 01:54:13 +0000 (18:54 -0700)
committerYehuda Katz <wycats@gmail.com>
Tue, 24 Jun 2014 01:54:13 +0000 (18:54 -0700)
This will become moot once we have a lockfile, because we'll be able to
check whether the last rev we used is still available and skip fetching.

src/bin/cargo-compile.rs
src/bin/cargo-git-checkout.rs
src/cargo/core/registry.rs
src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_rustc.rs
src/cargo/sources/git/source.rs
src/cargo/sources/git/utils.rs
src/cargo/util/config.rs
tests/test_cargo_compile_git_deps.rs

index 818d52ec1bb125efc60b84fa78e8c15c7f63e9e5..c097f6f2b56a14af2d684ac3afec987793093852 100644 (file)
@@ -20,10 +20,13 @@ use cargo::util::important_paths::find_project;
 
 #[deriving(PartialEq,Clone,Decodable,Encodable)]
 pub struct Options {
-    manifest_path: Option<String>
+    manifest_path: Option<String>,
+    update_remotes: bool
 }
 
-hammer_config!(Options "Compile the current project")
+hammer_config!(Options "Compile the current project", |c| {
+    c.short("update_remotes", 'u')
+})
 
 fn main() {
     execute_main_without_stdin(execute);
@@ -43,7 +46,9 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
                     }))
     };
 
-    ops::compile(&root, shell).map(|_| None).map_err(|err| {
+    let update = options.update_remotes;
+
+    ops::compile(&root, update, shell).map(|_| None).map_err(|err| {
         CliError::from_boxed(err, 101)
     })
 }
index 64fefed8ca800f6fc66474ee87458f9714dc43b7..699d0a411e8372de8c71611399ff333eb228004e 100644 (file)
@@ -37,7 +37,7 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
 
     let source_id = SourceId::for_git(&url, reference.as_slice());
 
-    let mut config = try!(Config::new(shell).map_err(|e| CliError::from_boxed(e, 1)));
+    let mut config = try!(Config::new(shell, true).map_err(|e| CliError::from_boxed(e, 1)));
     let mut source = GitSource::new(&source_id, &mut config);
 
     try!(source.update().map_err(|e| {
index 169137df52b659dc71805c875963a2df2f2e9ecd..f150c7880234915858b5eb022c0b278129274a07 100644 (file)
@@ -1,5 +1,5 @@
 use std::vec::Vec;
-use core::{MultiShell, Source, SourceId, Summary, Dependency, PackageId, Package};
+use core::{Source, SourceId, Summary, Dependency, PackageId, Package};
 use util::{CargoResult, ChainError, Config, human};
 
 pub trait Registry {
@@ -20,15 +20,15 @@ pub struct PackageRegistry<'a> {
     overrides: Vec<Summary>,
     summaries: Vec<Summary>,
     searched: Vec<SourceId>,
-    shell: &'a mut MultiShell
+    config: &'a mut Config<'a>
 }
 
 impl<'a> PackageRegistry<'a> {
     pub fn new<'a>(source_ids: Vec<SourceId>,
                override_ids: Vec<SourceId>,
-               shell: &'a mut MultiShell) -> CargoResult<PackageRegistry<'a>> {
+               config: &'a mut Config<'a>) -> CargoResult<PackageRegistry<'a>> {
 
-        let mut reg = PackageRegistry::empty(shell);
+        let mut reg = PackageRegistry::empty(config);
 
         for id in source_ids.iter() {
             try!(reg.load(id, false));
@@ -41,13 +41,13 @@ impl<'a> PackageRegistry<'a> {
         Ok(reg)
     }
 
-    fn empty<'a>(shell: &'a mut MultiShell) -> PackageRegistry<'a> {
+    fn empty<'a>(config: &'a mut Config<'a>) -> PackageRegistry<'a> {
         PackageRegistry {
             sources: vec!(),
             overrides: vec!(),
             summaries: vec!(),
             searched: vec!(),
-            shell: shell
+            config: config
         }
     }
 
@@ -82,7 +82,7 @@ impl<'a> PackageRegistry<'a> {
     fn load(&mut self, namespace: &SourceId, override: bool) -> CargoResult<()> {
 
         (|| {
-            let mut source = namespace.load(&mut try!(Config::new(self.shell)));
+            let mut source = namespace.load(self.config);
             let dst = if override {&mut self.overrides} else {&mut self.summaries};
 
             // Ensure the source has fetched all necessary remote data.
index ca935f4b4213a5ea7e8688214864f8e1985678f1..308a1146a7605e4fa5970219d294384ded203eaa 100644 (file)
 //!
 
 use std::os;
-use util::config::{ConfigValue};
+use util::config::{Config, ConfigValue};
 use core::{MultiShell, Source, SourceId, PackageSet, resolver};
 use core::registry::PackageRegistry;
 use ops;
 use sources::{PathSource};
 use util::{CargoResult, Wrap, config, internal, human};
 
-pub fn compile(manifest_path: &Path, shell: &mut MultiShell) -> CargoResult<()> {
+pub fn compile(manifest_path: &Path, update: bool, shell: &mut MultiShell) -> CargoResult<()> {
     log!(4, "compile; manifest-path={}", manifest_path.display());
 
     let mut source = PathSource::for_path(&manifest_path.dir_path());
@@ -45,8 +45,10 @@ pub fn compile(manifest_path: &Path, shell: &mut MultiShell) -> CargoResult<()>
     let source_ids = package.get_source_ids();
 
     let packages = {
+        let mut config = try!(Config::new(shell, update));
+
         let mut registry =
-            try!(PackageRegistry::new(source_ids, override_ids, shell));
+            try!(PackageRegistry::new(source_ids, override_ids, &mut config));
 
         let resolved =
             try!(resolver::resolve(package.get_dependencies(), &mut registry));
@@ -58,7 +60,8 @@ pub fn compile(manifest_path: &Path, shell: &mut MultiShell) -> CargoResult<()>
 
     debug!("packages={}", packages);
 
-    try!(ops::compile_packages(&package, &PackageSet::new(packages.as_slice()), shell));
+    let mut config = try!(Config::new(shell, update));
+    try!(ops::compile_packages(&package, &PackageSet::new(packages.as_slice()), &mut config));
 
     Ok(())
 }
index 24d171c0bf6599002dd7108e33d8678defb1a99f..b28984080f490ad29dafab3095cdc5d463e286d0 100644 (file)
@@ -10,20 +10,20 @@ use util::{Config};
 
 type Args = Vec<String>;
 
-struct Context<'a> {
+struct Context<'a, 'b> {
     dest: &'a Path,
     deps_dir: &'a Path,
     primary: bool,
     rustc_version: &'a str,
     compiled_anything: bool,
-    config: &'a mut Config<'a>
+    config: &'b mut Config<'b>
 }
 
-pub fn compile_packages(pkg: &Package, deps: &PackageSet,
-                        shell: &mut MultiShell) -> CargoResult<()> {
+pub fn compile_packages<'a>(pkg: &Package, deps: &PackageSet,
+                        config: &'a mut Config<'a>) -> CargoResult<()> {
+
     debug!("compile_packages; pkg={}; deps={}", pkg, deps);
 
-    let mut config = try!(Config::new(shell));
     let target_dir = pkg.get_absolute_target_dir();
     let deps_target_dir = target_dir.join("deps");
 
@@ -47,7 +47,7 @@ pub fn compile_packages(pkg: &Package, deps: &PackageSet,
         primary: false,
         rustc_version: rustc_version.as_slice(),
         compiled_anything: false,
-        config: &mut config
+        config: config
     };
 
     // Traverse the dependencies in topological order
index 3894183b9aab2b953b63252e238ef8355498eb52..aa889f92f80c5fb510500b44a8c61550e3199c06 100644 (file)
@@ -92,11 +92,20 @@ impl<'a, 'b> Show for GitSource<'a, 'b> {
 
 impl<'a, 'b> Source for GitSource<'a, 'b> {
     fn update(&mut self) -> CargoResult<()> {
-        try!(self.config.shell().status("Updating",
-            format!("git repository `{}`", self.remote.get_url())));
+        let should_update = self.config.update_remotes() || {
+            !self.remote.has_ref(&self.db_path, self.reference.as_slice()).is_ok()
+        };
+
+        let repo = if should_update {
+            try!(self.config.shell().status("Updating",
+                format!("git repository `{}`", self.remote.get_url())));
+
+            log!(5, "updating git source `{}`", self.remote);
+            try!(self.remote.checkout(&self.db_path))
+        } else {
+            self.remote.db_at(&self.db_path)
+        };
 
-        log!(5, "updating git source `{}`", self.remote);
-        let repo = try!(self.remote.checkout(&self.db_path));
         try!(repo.copy_to(self.reference.as_slice(), &self.checkout_path));
 
         self.path_source.update()
index 761d9401dbf9d83fb94887c6c3d731f4f9fdebe5..5d560a39306b5d3fdae5fdb4a459d8561a82e088 100644 (file)
@@ -50,13 +50,13 @@ macro_rules! git(
 )
 
 macro_rules! git_output(
-    ($config:expr, $str:expr, $($rest:expr),*) => (
+    ($config:expr, $str:expr, $($rest:expr),*) => ({
         try!(git_output(&$config, format!($str, $($rest),*)))
-    );
+    });
 
-    ($config:expr, $str:expr) => (
+    ($config:expr, $str:expr) => ({
         try!(git_output(&$config, format!($str)))
-    );
+    });
 )
 
 macro_rules! errln(
@@ -146,6 +146,11 @@ impl GitRemote {
         &self.url
     }
 
+    pub fn has_ref<S: Str>(&self, path: &Path, reference: S) -> CargoResult<()> {
+        git_output!(*path, "rev-parse {}", reference.as_slice());
+        Ok(())
+    }
+
     pub fn checkout(&self, into: &Path) -> CargoResult<GitDatabase> {
         if into.exists() {
             try!(self.fetch_into(into));
index 9dcad1175e337cac48f206c63d9275c3c483a2c5..7c0ac0a140ee279f0595ebe9801ce92a234e87a2 100644 (file)
@@ -9,16 +9,18 @@ use cargo_toml = util::toml;
 
 pub struct Config<'a> {
     home_path: Path,
+    update_remotes: bool,
     shell: &'a mut MultiShell
 }
 
 impl<'a> Config<'a> {
-    pub fn new<'a>(shell: &'a mut MultiShell) -> CargoResult<Config<'a>> {
+    pub fn new<'a>(shell: &'a mut MultiShell, update_remotes: bool) -> CargoResult<Config<'a>> {
         Ok(Config {
             home_path: try!(os::homedir().require(|| {
                 human("Cargo couldn't find your home directory. \
                       This probably means that $HOME was not set.")
             })),
+            update_remotes: update_remotes,
             shell: shell
         })
     }
@@ -34,6 +36,10 @@ impl<'a> Config<'a> {
     pub fn shell<'a>(&'a mut self) -> &'a mut MultiShell {
         &mut *self.shell
     }
+
+    pub fn update_remotes(&mut self) -> bool {
+        self.update_remotes
+    }
 }
 
 #[deriving(Eq,PartialEq,Clone,Encodable,Decodable)]
index a89fd713551470688ec7277f038cf4533efc75f5..a5ae216874ed6f3ca4237a9760680ca0db564488 100644 (file)
@@ -222,25 +222,33 @@ test!(recompilation {
                                             UPDATING, git_project.root().display(),
                                             COMPILING, git_project.root().display(),
                                             COMPILING, p.root().display())));
+
     // Don't recompile the second time
     assert_that(p.process("cargo-compile"),
-                execs().with_stdout(format!("{} git repository `file:{}`\n\
-                                             {} bar v0.5.0 (file:{})\n\
+                execs().with_stdout(format!("{} bar v0.5.0 (file:{})\n\
                                              {} foo v0.5.0 (file:{})\n",
-                                            UPDATING, git_project.root().display(),
                                             FRESH, git_project.root().display(),
                                             FRESH, p.root().display())));
+
     // Modify a file manually, shouldn't trigger a recompile
     File::create(&git_project.root().join("src/bar.rs")).write_str(r#"
         pub fn bar() { println!("hello!"); }
     "#).assert();
+
     assert_that(p.process("cargo-compile"),
+                execs().with_stdout(format!("{} bar v0.5.0 (file:{})\n\
+                                             {} foo v0.5.0 (file:{})\n",
+                                            FRESH, git_project.root().display(),
+                                            FRESH, p.root().display())));
+
+    assert_that(p.process("cargo-compile").arg("-u"),
                 execs().with_stdout(format!("{} git repository `file:{}`\n\
                                              {} bar v0.5.0 (file:{})\n\
                                              {} foo v0.5.0 (file:{})\n",
                                             UPDATING, git_project.root().display(),
                                             FRESH, git_project.root().display(),
                                             FRESH, p.root().display())));
+
     // Commit the changes and make sure we trigger a recompile
     File::create(&git_project.root().join("src/bar.rs")).write_str(r#"
         pub fn bar() { println!("hello!"); }
@@ -248,7 +256,8 @@ test!(recompilation {
     git_project.process("git").args(["add", "."]).exec_with_output().assert();
     git_project.process("git").args(["commit", "-m", "test"]).exec_with_output()
                .assert();
-    assert_that(p.process("cargo-compile"),
+
+    assert_that(p.process("cargo-compile").arg("-u"),
                 execs().with_stdout(format!("{} git repository `file:{}`\n\
                                              {} bar v0.5.0 (file:{})\n\
                                              {} foo v0.5.0 (file:{})\n",